package t3invoke;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t3bean.ParkingRecordBean;
import t3bean.ParkingSpaceBean;
import t3dao.ParkingRecordDao;
import t3dao.ParkingSpaceDao;

public class ParkCarAction implements Action {

	@Override
	public void parkCar(Scanner sc) {
		System.out.println("输入车牌号");
		String carid = sc.nextLine();
		System.out.println("输入车高");
		int height = Integer.parseInt(sc.nextLine());
		if (height >= 3) {
			System.out.println("超出高度");
			return;
		}
		if (carid.equals(" ") || carid.contentEquals("")) {
			System.out.println("无牌照");
			return;
		}
		ParkingSpaceDao psd = new ParkingSpaceDao();
		List<ParkingSpaceBean> list = new ArrayList<>();
		int n = 0;
		for (int i = 1; i <= 10; i++) {
			ParkingSpaceBean psb = new ParkingSpaceBean();
			psb.setSpaceId(i);
			try {
				list = psd.select(psb);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			ParkingSpaceBean psb1 = list.get(0);
			if (psb1.getIsParking() == 0) {
				psb1.setIsParking(1);
				psb1.setCarid(carid);
				System.out.println(psb1.getCarid());
				psd.update(psb1);
				n = 1;
				break;
			}
		}
//		n=setIsParking(list,psd,n);
		if (n == 0) {
			System.out.println("车位停满了");
			return;
		}

		ParkingRecordBean prb = new ParkingRecordBean();
		prb.setCarId(carid);

		prb.setSpaceId(list.get(0).getSpaceId());
		System.out.println("车位号为" + prb.getSpaceId());
		LocalDateTime date = LocalDateTime.now();

		prb.setParkingTime(date);
		ParkingRecordDao prd = new ParkingRecordDao();
		prd.insert(prb);

	}
//	int setIsParking(List<ParkingSpaceBean> list,ParkingSpaceDao psd,int n) {
//		
//		return n;
//	}

	@Override
	public void showParkingSpace() {
	}

	@Override
	public void getCar(Scanner sc) {
	}

	@Override
	public void showMoneyRecord() {
	}

}
